package main

import (
	pb "gitee.com/micro-services/protocol/verify/server"
	"github.com/micro/go-micro/v2"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-plugins/registry/etcdv3/v2"
	"log"
	"micro.verify.server/config"
	"micro.verify.server/controller"
	"micro.verify.server/database"
	"micro.verify.server/wrapper"
	"time"
)

func main() {
	reg := etcdv3.NewRegistry(func(opts *registry.Options) {
		// etcd 集群地址
		opts.Addrs = config.Endpoint
	})

	// micro服务
	service := micro.NewService(
		micro.Name("go.micro.verify.server"),
		micro.Registry(reg),
		micro.RegisterTTL(time.Second*30),      // 服务注册TTL生存期
		micro.RegisterInterval(time.Second*10), // N秒重新注册一次
	)

	err := config.InitCfg("/micro/verify", config.AppConf)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("GConf:%+v", config.GConf)

	database.InitRedis()
	defer database.Close()

	service.Init(
		micro.WrapHandler(wrapper.WrapperTracer()),
	)

	_ = pb.RegisterCaptchaServerHandler(service.Server(), new(controller.CaptchaController))
	_ = pb.RegisterEmailServerHandler(service.Server(), new(controller.EmailController))

	if err := service.Run(); err != nil {
		log.Fatal(err)
	}
}
